using System;
using System.Collections.Generic;
using System.Linq;

namespace CF_DAL_WS.Repositories
{
	#region using
	using CF_DAL_WS.Models;
	using CF_DAL_WS.Helpers;
	#endregion
	public class CartRepository
	{
        public List<Cart> SelectByCartID(string cartid)
        {
            try
            {
                return DataContext.Instance.Cart.Where(item => item.CartId == cartid).ToList();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        public Cart SelectByRecordID(int recordid)
        {
            try
            {
                return DataContext.Instance.Cart.FirstOrDefault(item => item.RecordId == recordid);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        public Cart SelectByIDAndRecordID(string cartid, int recordid)
        {
            try
            {
                return DataContext.Instance.Cart.FirstOrDefault(item => item.CartId == cartid && item.RecordId == recordid);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

		public Cart SelectByIDAndFood(string cartid, int foodid)
		{
			try
			{
				return DataContext.Instance.Cart.FirstOrDefault(item => item.CartId == cartid && item.FoodID == foodid );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public bool Insert(Cart obj)
		{
			try
			{
				DataContext.Instance.Cart.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Delete(string cartid, int recordid)
		{
			try
			{
                var delitem = DataContext.Instance.Cart.FirstOrDefault(item => item.CartId == cartid && item.RecordId == recordid);
				DataContext.Instance.Cart.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

        public bool Empty(string cartid)
        {
            try
            {
                var lstDel = DataContext.Instance.Cart.Where(item => item.CartId == cartid).ToList();
                foreach (var item in lstDel)
                {
                    DataContext.Instance.Cart.DeleteObject(item);
                }
                DataContext.Instance.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return false;
        }

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}
	}
}